﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace Yule8xCommon.Applib
{
    public class HttpUtil
    {
        public static string HttpGet(string strUrl, string strParams = "", string encoding = "utf-8")
        {
            if (!string.IsNullOrWhiteSpace(strParams))
            {
                strUrl = strUrl + "?" + strParams;
            }
            string strMsg = string.Empty;
            try
            {
                WebRequest request = WebRequest.Create(strUrl);
                request.Method = "GET";
                request.ContentType = "text/html;charset=" + encoding.ToUpper();
                using (WebResponse response = request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
                    {
                        strMsg = reader.ReadToEnd();
                    }
                }
            }
            catch
            { }
            return strMsg;
        }
        public static byte[] HttpGetBytes(string strUrl, string strParams = "")
        {
            if (!string.IsNullOrWhiteSpace(strParams))
            {
                strUrl = strUrl + "?" + strParams;
            }
            byte[] bytes = null;
            try
            {
                WebRequest request = WebRequest.Create(strUrl);
                request.Method = "GET";
                request.ContentType = "image/png";
                WebResponse response = request.GetResponse();

                using (Stream stm = response.GetResponseStream())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        System.Drawing.Image image = System.Drawing.Image.FromStream(stm);
                        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        bytes = ms.ToArray();
                    }
                }
                response.Close();
            }
            catch
            { }
            return bytes;
        }

        public static string HttpPost(string postUrl, string paramData, string encodingStr = "utf-8", CookieContainer cookie = null)
        {
            string ret = string.Empty;
            try
            {
                Encoding encoding = string.IsNullOrEmpty(encodingStr) || "default" == encodingStr ? Encoding.Default : Encoding.GetEncoding(encodingStr);
                byte[] byteArray = encoding.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.ContentLength = byteArray.Length;
                webReq.CookieContainer = cookie;

                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                if (null != cookie)
                {
                    response.Cookies = cookie.GetCookies(response.ResponseUri);
                }
                StreamReader sr = new StreamReader(response.GetResponseStream(), encoding);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                LogUtil.Instance.WriteError("[HttpPost] " + postUrl, ex);
            }
            return ret;
        }
    }
}